package com.dajie.pt.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBconn {
    private static Logger logger = LoggerFactory.getLogger(DBconn.class);
    private static Connection connBSS = null;
    private static Connection connSTATE = null;

    public DBconn() {
    }

    public static List<String> getAllUnsubType() {
        Statement statement = null;
        ArrayList res = new ArrayList();

        try {
            statement = connBSS.createStatement();
            ResultSet e = statement.executeQuery("select distinct value from business_type where id >= 2000 and id< 4000");

            while (e.next()) {
                String value = e.getString(1);
                res.add(value);
            }

            logger.info("DBconn --> getAllUnsubType get category:{} size:{}", res, Integer.valueOf(res.size()));
        } catch (SQLException var4) {
            logger.error("{}", var4.getMessage());
        }

        return res;
    }

    public static int writeUnsub(String uid, String toEmail) {
        List res = getAllUnsubType();
        Statement statement = null;
        int count = 0;

        try {
            statement = connSTATE.createStatement();
            String sql;
            for (Iterator e = res.iterator(); e.hasNext(); count = statement.executeUpdate(sql)) {
                String category = (String) e.next();
                sql = String.format("INSERT INTO tb_blacklist (uid,email,domain,category,template,create_date) VALUES (%s, \'%s\', \'%s\', \'%s\', \'%s\', now()) ON DUPLICATE KEY UPDATE create_date = now();", new Object[]{uid, toEmail, !toEmail.isEmpty() ? toEmail.split("@")[1] : "", category, category});
                logger.info("DBconn --> writeUnsub sql:{}", sql);
            }
        } catch (SQLException var8) {
            logger.error("{}", var8);
        }

        return count;
    }

    public static void main(String[] args) {
        getAllUnsubType();
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connBSS = DriverManager.getConnection("jdbc:mysql://cobarVIP:12001/DB_BSS?autoCommit=true&autoReconnect=true&useUnicode=true&tinyInt1isBit=false&zeroDateTimeBehavior=round&characterEncoding=UTF-8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull", "platformol", "platformol-3");
            connSTATE = DriverManager.getConnection("jdbc:mysql://cobarVIP:12001/DB_STAT_3308PT?autoCommit=true&autoReconnect=true&useUnicode=true&tinyInt1isBit=false&zeroDateTimeBehavior=round&characterEncoding=UTF-8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull", "platformol", "platformol-3");
        } catch (ClassNotFoundException var1) {
            var1.printStackTrace();
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

    }
}
